package com.lc.luzhengtang.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.PhotoShowActivity;
import com.lc.luzhengtang.banner.BannerLayout;
import com.lc.luzhengtang.bean.ShopDetailBean;
import com.lc.luzhengtang.conn.Conn;
import com.lc.luzhengtang.conn.ConnShopDetail;

import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * <b>Project:</b> SlideDetailsLayout<br>
 * <b>Create Date:</b> 16/1/25<br>
 * <b>Author:</b> Gordon<br>
 * <b>Description:</b> <br>
 */
public class ListFragment extends BaseFragment {
    String mTitle;

    String result;
    @BindView(R.id.banner2)
    BannerLayout banner2;
    Unbinder unbinder;
    @BindView(R.id.text_title)
    TextView textTitle;
    @BindView(R.id.text_money)
    TextView textMoney;
    @BindView(R.id.text_order_pay)
    TextView textOrderPay;
    @BindView(R.id.text_xiaoliang)
    TextView textXiaoliang;
    @BindView(R.id.img_help)
    ImageView imgHelp;

    public ListFragment() {

    }

    public static ListFragment getInstance(String title) {
        ListFragment sf = new ListFragment();
        sf.mTitle = title;
        return sf;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.layout_listview, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final ListView listView = (ListView) view.findViewById(android.R.id.list);
        Bundle bundle = getArguments();
        result = bundle.getString("shop_id");
        getData();
        Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show();
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            datas.add("data: " + i);
        }

        final View footView = getActivity().getLayoutInflater()
                .inflate(R.layout.slidedetails_marker_default_layout, null);
        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                open(true);
            }
        });

        listView.addFooterView(footView);

        listView.setAdapter(new Adapter(datas));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.text_title, R.id.text_money, R.id.text_order_pay, R.id.text_xiaoliang, R.id.img_help})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_title:
                break;
            case R.id.text_money:
                break;
            case R.id.text_order_pay:
                break;
            case R.id.text_xiaoliang:
                break;
            case R.id.img_help:
                break;
        }
    }

    private class Adapter extends BaseAdapter {

        private List<String> datas;

        Adapter(List<String> datas) {
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return null == datas ? 0 : datas.size();
        }

        @Override
        public String getItem(int position) {
            return null == datas ? null : datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (null == convertView) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.layout_list_item, null);
            }
            final TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
            textView.setText(getItem(position));
            return convertView;
        }
    }


    public void getData() {
        new ConnShopDetail(result, BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<ShopDetailBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, ShopDetailBean shopDetailBean) throws Exception {
                super.onSuccess(toast, type, o, shopDetailBean);

                textTitle.setText(shopDetailBean.getData().getGoodsinfo().getTitle());
                textXiaoliang.setText(shopDetailBean.getData().getGoodsinfo().getInfo());
                textMoney.setText("￥" + shopDetailBean.getData().getGoodsinfo().getMarket_price());
                //  shangpin_id = shopDetailBean.getData().getGoodsinfo().getId() + "";
                textOrderPay.setText("运费：" + shopDetailBean.getData().getGoodsinfo().getMarket_price());
                textXiaoliang.setText("销量：" + shopDetailBean.getData().getGoodsinfo().getVolume());


                List<String> urls = new ArrayList<>();
                final List<String> urlspic = new ArrayList<String>();
                urls = shopDetailBean.getData().getGoodsinfo().getPicarr();
                for (int i = 0; i < urls.size(); i++) {
                    urlspic.add(Conn.HOST3 + urls.get(i));
                }



                banner2.setViewUrls(urlspic);
                final List<String> finalUrls = urls;
                banner2.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        //Toast.makeText(ShopDetailActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show();

                        Intent intent = new Intent();

                        //intent.putExtra("url", urlspic.get(position));
                        intent.putStringArrayListExtra("mImages", (ArrayList<String>) urlspic);
                        intent.setClass(getActivity(), PhotoShowActivity.class);
                        startActivity(intent);

                    }
                });


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
            }
        }


        ).execute(getActivity(), false);
    }
}
